/*===============================
 *Title      :  FileBean.java
 *Created  :[ 2012-3-21 下午4:01:49 ] by fanlt
 * ==============================
 * Description:   
 * Copyright  :  Copyright (c) 2012
 * Company    :  Flyfot
 * @author  <a href="fanlt@flyfot.cn">fanlt</a>
 *
 */
package org.zxstudio.encrypt.tree;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;


/**
 * @author fan
 * 
 */
public class FileBean {

	private File file;
	private String name;
	private String stubName;
	private String stubParentName; // encrypt parent path
	private String parentName; // parent path

	private FileBean parentBean;
	private String path;
	// private StringBuilder parentName = new StringBuilder();
	private boolean hasChild;
	private boolean hasDirChild;

	public FileBean(File file, String parent) {
		if (file != null) {
			this.file = file;
			name = file.getName();
			stubParentName = parent;
			parentName = file.getParent();
			path = file.getAbsolutePath();
			if (file.isDirectory()) {
				stubName = name;
			} else {
				stubName = org.zxstudio.encrypt.utils.MD5.MD5(name);
			}
		}
	}

	public FileBean(File file) {
		this.file = file;
		name = file.getName();
		parentName = file.getParent();
		path = file.getAbsolutePath();
	}

	public String getStubName() {
		return stubName;
	}

	public void setStubName(String stubName) {
		this.stubName = stubName;
	}

	public String getStubParentName() {
		return stubParentName;
	}

	public void setStubParentName(String stubParentName) {
		this.stubParentName = stubParentName;
	}

	public String getParentName() {
		return parentName;
	}

	public void setParentName(String parentName) {
		this.parentName = parentName;
	}

	/**
	 * 文件夹路径获取
	 * 
	 * @return
	 */
	public String getStubPath() {
		final StringBuilder stubPath = new StringBuilder();
		if (isDirectory()) {
			stubPath.append(stubParentName);
			stubPath.append(stubName);
			stubPath.append("/");
		} else {
			stubPath.append(stubParentName);
			stubPath.append(stubName);
		}
		return stubPath.toString();
	}

	/**
	 * 文件的加密路径获取
	 * 
	 * @return
	 */
	/*public String getFileStubPath() {
		final StringBuilder stubPath = new StringBuilder();
	
		return stubPath.toString();
	}*/

	public boolean isHasChild() {
		return hasChild;
	}

	private void setHasChild(boolean hasChild) {
		this.hasChild = hasChild;
	}

	public boolean isHasDirChild() {
		return hasDirChild;
	}

	private void setHasDirChild(boolean hasDirChild) {
		this.hasDirChild = hasDirChild;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public boolean isDirectory() {
		return (file != null ? file.isDirectory() : false);
	}

	public boolean isFile() {
		return (file != null ? !isDirectory() : false);
	}

	public ArrayList<FileBean> getChildList() {
		File[] childArray = file.listFiles();
		ArrayList<FileBean> childList = new ArrayList<FileBean>();
		childList.clear();
		if (childArray != null) {
			for (File f : childArray) {
				final StringBuilder sb = new StringBuilder();
				sb.append(stubParentName);
				sb.append(name);
				sb.append("/");
				FileBean bean = new FileBean(f, sb.toString());
				bean.parentBean = this;
				childList.add(bean);
			}
		}
		return childList;
	}

	public FileBean getParentBean() {
		return parentBean;
	}

	public void setParentBean(FileBean parentBean) {
		this.parentBean = parentBean;
	}

	public boolean hasChildren() {
		return (getChildList().size() > 0) ? true : false;
	}
}